package main

import (
	"fmt"
	"log"
	"net/http"
	"path/filepath"

	"github.com/gin-gonic/gin"
)

func main() {
	r := gin.Default()
	//设置表单限制上传文件大小为8M
	r.MaxMultipartMemory = 8 << 20
	r.POST("/upload", func(c *gin.Context) {
		//单文件
		file, _ := c.FormFile("file")
		log.Println(file.Filename)

		//上传到指定路径
		filename, err := filepath.Abs("D:\\uploadfiles\\" + file.Filename) //获取文件的绝对路径也就是将该文件上传到指定绝对路径下，切记!路径尾要加上文件名
		if err != nil {
			log.Println("File upload Fail")
		}
		if filepath.IsAbs(filename) {
			log.Println(filename)
		}
		c.SaveUploadedFile(file, filename)
		c.String(http.StatusOK, fmt.Sprintf("%s upload ok!", file.Filename))
	})
	r.Run(":8081")
}

//curl -X POST "http://localhost:8081/upload" -F "files=@girl.jpg" -H "Content-Type: multipart/form-data"
